package com.clothing.customization.controller;

import com.clothing.customization.common.ApiResponse;
import com.clothing.customization.common.PageResult;
import com.clothing.customization.entity.ClothingAccessory;
import com.clothing.customization.service.ClothingAccessoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

@RestController
@RequestMapping("/clothing/accessory")
@Tag(name = "服装配饰管理", description = "服装配饰增删改查接口")
public class ClothingAccessoryController {

    @Autowired
    private ClothingAccessoryService accessoryService;

    @GetMapping("/list")
    @Operation(summary = "获取配饰列表", description = "分页获取配饰列表，支持按名称、类型和状态筛选")
    public ApiResponse<PageResult<ClothingAccessory>> list(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "配饰名称") @RequestParam(required = false) String name,
            @Parameter(description = "配饰类型") @RequestParam(required = false) String type,
            @Parameter(description = "状态(1-启用 0-禁用)") @RequestParam(required = false) Integer status
    ) {
        PageResult<ClothingAccessory> result = accessoryService.getAccessoryList(pageNum, pageSize, name, type, status);
        return ApiResponse.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取配饰详情", description = "根据ID获取配饰详情")
    public ApiResponse<ClothingAccessory> getById(@PathVariable Long id) {
        ClothingAccessory accessory = accessoryService.getById(id);
        return ApiResponse.success(accessory);
    }

    @PostMapping
    @Operation(summary = "添加配饰", description = "添加新的服装配饰")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ApiResponse<ClothingAccessory> add(@Valid @RequestBody ClothingAccessory accessory) {
        ClothingAccessory result = accessoryService.addAccessory(accessory);
        return ApiResponse.success(result);
    }

    @PutMapping
    @Operation(summary = "更新配饰", description = "更新已有的服装配饰")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ApiResponse<ClothingAccessory> update(@Valid @RequestBody ClothingAccessory accessory) {
        ClothingAccessory result = accessoryService.updateAccessory(accessory);
        return ApiResponse.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除配饰", description = "根据ID删除服装配饰")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ApiResponse<Boolean> delete(@PathVariable Long id) {
        boolean result = accessoryService.deleteAccessory(id);
        return ApiResponse.success(result);
    }

    @PutMapping("/status")
    @Operation(summary = "更新配饰状态", description = "启用或禁用配饰")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ApiResponse<Boolean> updateStatus(
            @Parameter(description = "配饰ID") @RequestParam Long id,
            @Parameter(description = "状态(1-启用 0-禁用)") @RequestParam Integer status
    ) {
        boolean result = accessoryService.updateStatus(id, status);
        return ApiResponse.success(result);
    }
} 